// https://www.expressjs.com.cn/advanced/healthcheck-graceful-shutdown.html

/**
 * @title 开始清理资源，如数据库或文件
 */
function onSignal() {
  // console.log('server is starting cleanup'); // FIXME:
  return Promise.all([
    // your clean logic, like closing database connections
  ]);
}

/**
 * @title 清理完成,服务已关闭
 */
function onShutdown() {
  // console.log('cleanup finished, server is shutting down'); // FIXME:
}

/**
 * @title （可选）检查系统是否正常，例如db连接是否处于活动状态
 */
function healthCheck() {
  return Promise.resolve(
    // optionally include a resolve value to be included as
    // info in the health check response
    null,
  );
}

const options = {
  // health check options
  healthChecks: {
    '/healthcheck': healthCheck, // a function returning a promise indicating service health,
    verbatim: true, // [optional = false] use object returned from /healthcheck verbatim in response
  },
  caseInsensitive: false, // [optional] 给定的运行状况检查路由是否不区分大小写 (defaults to false)

  // cleanup options
  timeout: 1000, // [optional = 1000] 强制退出前的毫秒数
  signal: 'SIGTERM', // [optional = 'SIGTERM'] 相对于关机，要监听什么信号
  signals: ['SIGTERM'], // [optional = []] 要侦听的与关机相关的信号数组
  sendFailuresDuringShutdown: true, // [optional = true] 关机时是否发送失败（503）
  beforeShutdown: () => {
    // console.log('关闭之前'); // FIXME:
  }, // [optional] 在HTTP服务器开始关闭之前调用
  onSignal, // [optional] 清除函数，返回承诺（以前是 onSigterm ）
  onShutdown, // [optional] called right before exiting
  onSendFailureDuringShutdown: () => {
    // console.log('发送503之前'); // FIXME:
  }, // [optional] 在关机期间发送503之前调用

  // both
  // logger
  // [optional] 要调用的记录器函数有错误。记录器调用示例：`('error happened during shutdown', error)`。
};

module.exports = options;
